<template>
  <el-dialog
    :title="addOrXiu"
    :visible.sync="dialogVisible"
    width="45%"
    :before-close="handleClose"
  >
    <el-form ref="form" :model="form" label-width="150px" :rules="rules">
      <el-form-item label="型号名称:" prop="name" style="width: 550px">
        <el-input
          v-model="form.name"
          placeholder="请输入"
          maxlength="10"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="型号编码:" prop="model" style="width: 550px">
        <el-input
          v-model="form.model"
          placeholder="请输入（限制数字、字母、中划线、下划线）"
          maxlength="15"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="货道行数:" prop="vmRow" style="width: 550px">
        <el-input v-model.number="form.vmRow" placeholder="请输入">
          <template #suffix>
            <samp class="box">
              <el-button
                size="mini"
                :disabled="form.vmRow >= 10"
                class="el-icon-arrow-up buttTop"
                @click="form.vmRow++"
              />
              <el-button
                size="mini"
                class="el-icon-arrow-down"
                :disabled="form.vmRow <= 0"
                @click="form.vmRow--"
              />
            </samp>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="货道列数:" prop="vmCol" style="width: 550px">
        <el-input v-model.number="form.vmCol" placeholder="请输入">
          <template #suffix>
            <samp class="box">
              <el-button
                size="mini"
                :disabled="form.vmCol >= 10"
                class="el-icon-arrow-up buttTop"
                @click="form.vmCol++"
              />
              <el-button
                size="mini"
                class="el-icon-arrow-down"
                :disabled="form.vmCol <= 0"
                @click="form.vmCol--"
              />
            </samp>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item
        label="货道容量:"
        prop="channelMaxCapacity"
        style="width: 550px"
      >
        <el-input v-model.number="form.channelMaxCapacity" placeholder="请输入">
          <template #suffix>
            <samp class="box">
              <el-button
                size="mini"
                :disabled="form.channelMaxCapacity >= 10"
                class="el-icon-arrow-up buttTop"
                @click="form.channelMaxCapacity++"
              />
              <el-button
                size="mini"
                class="el-icon-arrow-down"
                :disabled="form.channelMaxCapacity <= 0"
                @click="form.channelMaxCapacity--"
              />
            </samp>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="设备图片:" prop="image">
        <el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.image" :src="form.image" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
        <span style="color: #bac0cd">支持扩展名：jpg、png，文件不得大于100kb</span>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="createNow">立即创建</el-button>
        <el-button @click="handleClose">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { upDatashouhuoji, addshouhuoji } from '../../../api/devive'
export default {
  name: 'Modify',
  data () {
    return {
      dialogVisible: false,
      form: {
        vmRow: null,
        vmCol: null,
        name: '',
        channelMaxCapacity: null,
        model: '',
        image: ''
      },
      rules: {
        vmRow: [{ required: true, message: '请输入', trigger: 'blur' }],
        vmCol: [{ required: true, message: '请输入', trigger: 'blur' }],
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        channelMaxCapacity: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        model: [{ required: true, message: '请输入', trigger: 'blur' }],
        image: [{ required: true, message: '请上传', trigger: 'blur' }]
      }
    }
  },
  computed: {
    addOrXiu () {
      return this.form.typeId ? '修改设备类型' : '添加设备类型'
    }
  },
  methods: {
    // 弹窗关闭时
    handleClose () {
      this.form = {
        vmRow: null,
        vmCol: null,
        name: '',
        channelMaxCapacity: null,
        model: '',
        image: ''
      }
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    // 上传图片
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 提交事件
    async createNow () {
      if (this.form.typeId) {
        const data = await upDatashouhuoji(this.form)
        console.log(data)
      } else {
        try {
          await addshouhuoji(this.form)
        } catch (error) {
          console.log(error)
        }
      }
      this.handleClose()
    }
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #ccc;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #000;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
.box {
  width: 50px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .el-button {
    width: 50px;
    height: 20px;
  }
  .el-button:first-child {
    margin-right: -10px;
  }
}
</style>
